package example.telosys.persistence;

import java.math.BigDecimal;
import java.util.List;

import org.demo.vo.bean.Book;
import org.telosys.persistence.PersistenceManager;
import org.telosys.persistence.PersistenceManagerFactory;
import org.telosys.persistence.Query;
import org.telosys.persistence.TelosysPersistence;

public class BookService { 

	protected PersistenceManager pm ;

	public BookService(PersistenceManager pm) {
		super();
		this.pm = pm;
	}
	
	
	public Book insertBook (String title, String isbn, int quantity, double price, int authorId, int publisherId) {
		Book book = new Book();
		book.setTitle(title);
		book.setIsbn(isbn);
		book.setQuantity(quantity);
		book.setPrice(new BigDecimal(price) );
		book.setAuthorId(authorId);
		book.setPublisherId(publisherId);
		
		//pm.insert(book);
		pm.insertKeyGen(book);
		return book ;
	}
	
	public Book findBook (int id) {
		Book book = new Book();
		book.setId(id);
		if ( pm.load(book) ) 
			return book ;
		else 
			return null ;
	}	
	
	public boolean removeBook (int id)  {
		Book book = new Book();
		book.setId(id);
		return pm.delete(book);
	}
	
	public List<Book> findAllBooks() {
		return pm.loadAll(Book.class);
	}
	
	public boolean updateBookTitle (int id, String title) 
	{
		Book book = new Book();
		book.setId(id);
		book.setTitle(title);
		return pm.update(book);
	}
	

//	public List<Customer> findCustomers(Book book) 
//	{
//		Query<Customer> query = pm.createQuery(Customer.class, 
//				Customer.$bookCode + " = ?", 
//				//"order by " + Customer.$firstName + " ASC");
//				orderBy(Customer.$firstName, Customer.$lastName) );
//		
//		System.out.println("QUERY SELECT = " + query.getSqlSelect() );
//		int n = query.getParamCount();
//		
//		System.out.println("All params initialized ? " + query.allParametersInitialized() );
//		
//		query.setParameter(1, book.getCode() );
//
//		System.out.println("All params initialized ? " + query.allParametersInitialized() );
//		
//		return pm.loadByQuery( query );
//		
//	}

	public int countAllBooks() 
	{
		return pm.countAll(Book.class);
	}


	public List<Book> findBooksByTitle (String title) 
	{
		Query<Book> query = pm.createQuery(Book.class, "$title like ?" );
		query.setParameter(1, title);
		
		return pm.loadByQuery( query );
	}
	
	public static void main( String args[] ) 
	{
		boolean b ;
		PersistenceManagerFactory pmf = TelosysPersistence.getPersistenceManagerFactory();
		PersistenceManager pm = pmf.createPersistenceManager();
		BookService service = new BookService(pm);
		
		System.out.println("pm.isOpen() : " + pm.isOpen() );
		
		System.out.println("pm.isAutoCommitMode() : " + pm.isAutoCommitMode() );
		
		b = service.removeBook(999);
		System.out.println("remove book : " + b );
		
		Book book = service.insertBook("My new book title", "ISBNaaaaaaa", 3, 34.50, 1, 2 );
		System.out.println("insert book : " + book );
		int id = book.getId();
		
		Book book2 = service.findBook(id);
		System.out.println("book found: " + book2 );
		
		System.out.println("count all : " + service.countAllBooks() );
		List<Book> list ;
		
		System.out.println("all books : " );
		list = service.findAllBooks();
		for ( Book c : list ) {
			System.out.println(" . " + c );
		}
		
		String titlePattern = "L%" ;
		System.out.println("books for name '" + titlePattern + "' :" );
		list = service.findBooksByTitle(titlePattern);
		for ( Book c : list ) {
			System.out.println(" . " + c );
		}
		
//		loadBookWithCustomers( service, "FR"  ) ;
		
		pm.close();
		System.out.println("pm.isOpen() : " + pm.isOpen() );
		
	}

//	public static void loadBookWithCustomers( BookService service, String key  ) 
//	{
//		System.out.println("Try to load book '" + key + "'");
//		Book book = service.findBook(key);
//		if ( book != null ) {
//			System.out.println("Book found : " + book );
//		}
//		else {
//			System.out.println("Book not found." );
//			return;
//		}
//		
//	}
}
